package com.eastseeway.radioproject.setting;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.LiveData;

import com.eastseeway.radioproject.base.BaseRepository;
import com.eastseeway.radioproject.base.Const;
import com.eastseeway.radioproject.database.RadioProjectDatabase;
import com.eastseeway.radioproject.database.UserInfoDao;
import com.eastseeway.radioproject.entities.OperationParams;
import com.eastseeway.radioproject.entities.UserInfo;
import com.eastseeway.radioproject.utils.CommandGenerator;
import com.eastseeway.radioproject.utils.SaveHelper;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SettingRepository extends BaseRepository {
    private final UserInfoDao mUserInfoDao;


    public SettingRepository(Context context) {
        mUserInfoDao = RadioProjectDatabase.getInstance(context).getUserInfoDao();
    }

    void addUser(UserInfo userInfo) {
        executorService.submit(() -> {
            long l = mUserInfoDao.addUser(userInfo);
            userInfo.setId((int) l);
            for (int i = 0; i < Const.POSITION_PREVIOUS_PARAMS; i++) {
                OperationParams operationParams = new OperationParams();
                operationParams.setId(CommandGenerator.generateId(userInfo.getId(),true,(i+1)));
                operationParams.setHasPressure(true);
                operationParams.setPosition(i+1);
                operationParams.setUsername(userInfo.getName());
                operationParams.setEmpty(true);
                mUserInfoDao.insertOperationParams(operationParams);
            }
            for (int i = 0; i < Const.POSITION_PREVIOUS_PARAMS; i++){
                OperationParams operationParams = new OperationParams();
                operationParams.setId(CommandGenerator.generateId(userInfo.getId(),false,(i+1)));
                operationParams.setHasPressure(false);
                operationParams.setPosition(i+1);
                operationParams.setUsername(userInfo.getName());
                operationParams.setEmpty(true);
                mUserInfoDao.insertOperationParams(operationParams);
            }
        });

        try {
            Runtime.getRuntime().exec("sync");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

//    删除某个用户后，删除对应的所有快捷参数
    void deleteUser(UserInfo userInfo){
        executorService.submit(()->{
            mUserInfoDao.deleteUser(userInfo);
            mUserInfoDao.deleteOperationParamsByName(userInfo.getName());
        });
    }

    void updateUser(UserInfo userInfo){
        executorService.submit(()->mUserInfoDao.updateUser(userInfo));
    }

    void deleteAll() {
        executorService.submit(mUserInfoDao::deleteAll);
    }

    LiveData<List<UserInfo>> queryAll() {
        return mUserInfoDao.queryAll();
    }

    void addRadioParams(OperationParams operationParams){
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                mUserInfoDao.insertOperationParams(operationParams);

            }
        });
    }


}
